<?php
namespace app\member\controller;

class Mall extends Member
{
    /**
     * 轮播
     */
    public function carousel()
    {
        if (request()->isPost()) {
            $data = input('post.');
            foreach ($data['imgpath'] as $key => $value) {
                $carousel[] = array(
                    'imgpath' => $value,
                );
            }
            $data['carousel'] = serializeMysql($carousel);
            $data['uid'] = UID;

            $res = dataUpdate(\tname::carousel, $data);
            if(!$res){
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $data = db(\tname::carousel)->where(array('uid' => UID, 'type' => 'mall'))->find();
            if (!empty($data)) {
                $data['carousel'] = serializeMysql($data['carousel'], 1);
            }
            $urlList = db(\tname::weixin_url)->where(array('uid' => UID, 'ishidden' => 0))->select();

            $this->assign('data', $data);
            $this->assign('urlList', $urlList);
            return $this->fetch();
        }
    }
    /**
     * 基础设置
     */
    public function config(){
        if (request()->isPost()) {
            $data = input('post.');
            $data['uid'] = UID;

            $res = dataUpdate('shuju_config', $data);
            if(!$res){
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $data = db('shuju_config')->where(array('uid' => UID))->find();
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
    /**
     * 订单列表 2017-10-15
     */
    public function order()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            if ($search['stime'] && !$search['etime']) {
                $where['createtime'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['createtime'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['createtime'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword'] !== null && $search['keyword'] !== '') {
                $where['nickname|name|phone|people'] = array('like', '%' . $search['keyword'] . '%');
            }
            session('where', $where);
            $dataList = db('shuju_order')->where($where)->order('createtime desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('mall/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }

        return $this->fetch();
    }

    /**
     * 订单详情    2017-10-15
     */
    public function orderdetail()
    {
        $id = input('param.id', 0);
        $data = db('shuju_order')->find($id);
        if ($id) {
            $data['return'] = serializeMysql($data['return'], 1);
            $data['returndata'] = serializeMysql($data['returndata'], 1);
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 订单列表导出    2017-10-15
     */
    public function orderexport()
    {
        $where = session('where');
        $dataList = db(\tname::mall_order)->where($where)->order('id desc')->select();
        foreach ($dataList as $key => $value) {
            switch ($value['status']) {
                case 0:
                    $value['status_'] = '待付款';
                    break;
                case 1:
                    $value['status_'] = '待发货';
                    break;
                case 2:
                    $value['status_'] = '已发货';
                    break;
                case 3:
                    $value['status_'] = '已完成';
                    break;
                case -1:
                    $value['status_'] = '已取消';
                    break;
            }
            $data[$key] = array(
                $value['nickname'],//昵称
                $value['ordernumber'],//订单编号
                $value['total_number'],//数量
                $value['total_price'],//价钱
                $value['pay_money'],//实际付款
                $value['linkman'],//联系人
                $value['linktel'],//联系电话
                $value['address'],//地址
                $value['express_name'],//快递名称
                $value['express_number'],//快递单号
                date('Y-m-d H:i:s', $value['create_time']),//时间
                $value['status_'],//状态
            );
        };

        $fileName = "订单表";
        $headArr = array("昵称", "订单编号", "数量", "价钱", "实际付款", "联系人", "联系电话", "地址", "快递名称", "快递单号", "时间", "状态");
        $msg = '状态：0代表未处理 1代表已处理';
        $res = getExcel($fileName, $headArr, $data, $msg);
        return $res;
    }
    /**
     * 用户列表    2017-10-15
     */
    public function vip()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'uid' => UID,
                'subscribe' => 1
            );
            if ($search['stime'] && !$search['etime']) {
                $where['subscribe_time'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['subscribe_time'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['subscribe_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword']) {
                $where['nickname|province|city|country'] = array('like', '%' . $search['keyword'] . '%');
            }
            session('where', $where);
            $dataList = db(\tname::vip)->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('mall/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }
        return $this->fetch();
    }
}